<?php

require_once( '../../libs/smarty.inc' );

require_once( '../../libs/smt_variables.php' );

require_once( '../../libs/Class_DB.php' );

require_once( '../../libs/Class_ERROR.php' );

require_once( '../../libs/login.inc.php' );



define('PAGE_MAX', 20 );



if( !in_array( "A", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;



$forms = $_GET;



if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){

	$page['current'] = $forms['page'];

	$st = ( $page['current'] -1 ) * PAGE_MAX;

}else{

	$page['current'] = 1;

	$st = 0;

}





$db = new Class_DB;



$sql = "SELECT * FROM D_CASE";

$cnt_sql = "SELECT COUNT(*) AS cnt FROM D_CASE";





$query	= "";



$where1 = "";

if( isset( $forms['title'] ) && $forms['title'] != ''){

	$where1 = "case_name LIKE '%" . $db->quote( $forms['title'] ) . "%' AND ";

	$query .= "&title=" . $forms['title'];

}

// 掲載日

$where2 = "";

if( isset( $forms['date_from'] ) && $forms['date_from'] != '' ){

	$where2 = sprintf( "case_date >= '%s 0:00:00' AND ", str_replace( '/', '-', $forms['date_from'] ) );

	$query .= "&date_from=" . $forms['date_from'];

}

$where3 = "";

if( isset( $forms['date_to'] ) && $forms['date_to'] != '' ){

	$where3 = sprintf( "case_date <= '%s 23:59:59' AND ", str_replace( '/', '-', $forms['date_to'] ) );

	$query .= "&date_to=" . $forms['date_to'];

}





if( $where1 . $where2 . $where3 != '' ){

	$sql .= " WHERE ";

	$sql .= $where1 . $where2 . $where3;

	$sql = substr( $sql, 0, strlen($sql) -5 );



	$cnt_sql .= " WHERE ";

	$cnt_sql .= $where1 . $where2 . $where3;

	$cnt_sql = substr( $cnt_sql, 0, strlen($cnt_sql) -5 );

}





$sql .=  sprintf(" ORDER BY case_date DESC, u_date DESC LIMIT %u, %u", $st, PAGE_MAX );



// 件数取得・ページング

$db->query( $cnt_sql );

$row = $db->fetch();

$page['cnt'] = $row['cnt'];

$page['end'] = floor( $page['cnt'] / PAGE_MAX );

if( $page['cnt'] % PAGE_MAX ) $page['end']++;



for( $i = 1; $i <= $page['end']; $i++ ){

	$page['paging'][] = $i;

}



$page['item'] = PAGE_MAX;

$page['query'] = $query;





$db->query( $sql );



while( $row = $db->fetch() ){

	$data[] = $row;

}





$smarty->assign('status',	array( 0 => '非公開', 1 => '公開' ));

$smarty->assign('page', $page);

if( isset($data) ) $smarty->assign('data', $data);

if( isset($forms) ) $smarty->assign('forms', $forms);

$smarty->display('web-admin/case_index.html');



?>